package com.matt.repository.impl;

import com.matt.foundation.DO.AdviseDetailDO;
import com.matt.foundation.DO.ChildrenCourseItemDO;
import com.matt.foundation.vo.IDCountVO;
import com.matt.repository.base.BaseNativeSqlRepository;
import com.matt.repository.base.ChildChildrenCourseItemRepository;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2017/9/11
 * *******************************
 */
@Component
public class ChildChildrenCourseItemRepositoryImpl extends BaseNativeSqlRepository implements ChildChildrenCourseItemRepository {

    @Override
    public List<ChildrenCourseItemDO> findByChildrenByCoursePlanId(String coursePlanId) {
        String findChildrenSql = String.format("select ci.id as id, c.name as name, ci.status as status, c.imgUrl as imgUrl, c.gender as gender, c.id as childrenId ,c.birthday" +
                        " from children_course_item ci LEFT join children c on ci.childrenId = c.id " +
                        " WHERE ci.coursePlanId = '%s' ",
                coursePlanId);
        List<ChildrenCourseItemDO> objectArrayList = sqlObjectList(findChildrenSql, ChildrenCourseItemDO.class);
        return objectArrayList;
    }

    @Override
    public Long countChildrenByCoursePlanId(String coursePlanId) {
        String findSql = String.format("select count(id)" +
                        " from children_course_item " +
                        " WHERE coursePlanId = '%s' ", coursePlanId);
        List objectArrayList = sqlSingleList(findSql);
        return Long.valueOf(objectArrayList.get(0).toString());
    }

    @Override
    public Map<String, Integer> countChildrenByCoursePlanIds(List<String> coursePlanIds){
        String findSql = "select coursePlanId as 'id', count(id) as 'count' from children_course_item WHERE coursePlanId in :1 group by coursePlanId ";

        if(coursePlanIds == null || coursePlanIds.size() == 0){
            return new HashMap<>();
        }

        List params = new ArrayList();
        params.add(coursePlanIds);

        List<IDCountVO> objectArrayList = sqlObjectList(findSql, params, IDCountVO.class);
        return IDCountVO.toMap(objectArrayList);
    }

    @Override
    public List<Object[]> findChildrenCourseNonEvaluateList(String teacherId, Integer itemStatus) {
        String findChildrenSql = String.format("select ci.id, p.courseName, p.earlyWeek, p.beginTime, p.endTime, p.courseDate, c.name, c.imgUrl ,ci.childrenId, ci.childrenCourseId " +
                        " from children_course_item ci LEFT JOIN course_plan p on ci.coursePlanId = p.id " +
                        " LEFT join Children c ON ci.childrenId = c.id" +
                        " WHERE ci.teacherId = '%s' and ci.status = '%s' and p.courseType=1 ",
                teacherId, itemStatus);
        List<Object[]> objectArrayList = sqlArrayList(findChildrenSql);
        return objectArrayList;
    }

    @Override
    public List<AdviseDetailDO> findAdviseByCoursePlanId(String coursePlanId) {
        String querySql =  String.format("select a.childrenName as name, a.content as content, c.imgUrl as imgUrl " +
                        " from children_course_item ci right join garden_leader_advise a on ci.childrenId = a.childrenId left join children c on a.childrenId = c.id " +
                        " WHERE ci.coursePlanId = '%s' ",
                coursePlanId);
        List<AdviseDetailDO> objectArrayList = sqlObjectList(querySql, AdviseDetailDO.class);
        return objectArrayList;
    }

    @Override
    public List<Object[]> findCompletedCourseByChildrenId(String childrenId) {
        String findChildrenSql = String.format("select ci.id, p.courseName, p.typeName,  p.courseDate, p.courseType " +
                        " from children_course_item ci LEFT JOIN course_plan p on ci.coursePlanId = p.id " +
                        " WHERE ci.childrenId = '%s' and (ci.status=2 or ci.status=5) " +
                        " ORDER BY p.courseDate desc ",
                childrenId);
        List<Object[]> objectArrayList = sqlArrayList(findChildrenSql);
        return objectArrayList;
    }
}
